//
//  MyFansViewController.m
//  Beauty
//
//  Created by Snow on 16/9/17.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "MyFansViewController.h"
#import "FansTableViewCell.h"
#import "ModelOfFans.h"
#import "GXMacro.h"
#import "LoginViewController.h"
#import "GXNetWorkingParser.h"
@interface MyFansViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tabelView;
@property (nonatomic, strong) NSMutableArray *array;
@end

@implementation MyFansViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    _tabelView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:_tabelView];
    _tabelView.delegate = self;
    _tabelView.dataSource = self;
    [_tabelView registerClass:[FansTableViewCell class] forCellReuseIdentifier:@"FansTableViewCell"];
}
- (void)handleData
{
    _array = [NSMutableArray array];
    //获取UserDefault
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *name = [userDefault objectForKey:@"name"];
    
    if (name == nil)
    {
        LoginViewController *login = [[LoginViewController alloc] init];
        NSLog(@"%@",name);
[self presentViewController:login animated:YES completion:^{
    
}];
        
    } else {
        NSString *password = [userDefault objectForKey:@"password"];
        NSString *url = [NSString stringWithFormat:@"http://www.wantease.com/mobile1/index.php?act=sns_friend&op=fans_list&key=%@", password];
        [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
            NSArray *arr = responseObject[@"datas"];
            for (NSDictionary *dic in arr) {
                ModelOfFans *model = [[ModelOfFans alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_array addObject:model];
            }
            [_tabelView reloadData];
        } filed:^(NSError *error) {
            
        }];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FansTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FansTableViewCell"];
    ModelOfFans *model = [_array objectAtIndex:indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.friend_tomavatar]];
    cell.labelOfName.text = model.member_truename;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
